/**
 * Created by zzy on 2017/1/13.
 */
var $ = require('jquery');
var $alert = require('libs/alert');
var $confirm = require('libs/confirm');
require('easyui');
var enable = {"0":"已停用","1":"已启用"};
var issys  = {"0":"否","1":"是"};
var isSale = require('libs/user-type').isSale;
var clickEvent = {
    modifyInit:function () {
        $("body").on("click","a[name=modify]",function () {
            var id = $(this).parents("tr").children(":first").text();
            window.location.href="role-update?id="+id;
        });
        return clickEvent;
    },
    startInit:function () {
        $("body").on("click","a[name=start]",function () {
            var id = $(this).parents("tr").children(":first").text();
            $confirm("确认启用该角色吗?",function () {
                $.ajax({
                    url:"api/v1/p/mms/authRole/enable",
                    type:"post",
                    data:JSON.stringify({"id":id, "enable":"1"}),
                    contentType: "application/json; charset=utf-8",
                    dataType:"json",
                    success:function (result) {
                        if(result.code=="200") {
                            $alert("启用成功!");
                            $("#main_table").datagrid("reload");
                        }else{
                            $alert(result.message);
                        }
                    }
                })
            })
        });
        return clickEvent;
    },
    stopInit:function () {
        $("body").on("click","a[name=stop]",function () {
            var id = $(this).parents("tr").children(":first").text();
            $confirm("确认停用该角色吗?",function () {
                $.ajax({
                    url:"api/v1/p/mms/authRole/enable",
                    type:"post",
                    data:JSON.stringify({"id":id,"enable":"0"}),
                    contentType: "application/json; charset=utf-8",
                    dataType:"json",
                    success:function (result) {
                        if(result.code=="200") {
                            $alert("停用成功!");
                            $("#main_table").datagrid("reload");
                        }else{
                            $alert(result.message);
                        }
                    }
                })
            })
        });
        return clickEvent;
    },
    deleteInit:function () {
        $("body").on("click","a[name=delete]",function () {
            var id = $(this).parents("tr").children(":first").text();
            $confirm("确认删除该角色吗?",function () {
                $.ajax({
                    url:"api/v1/p/mms/authRole/delete",
                    type:"post",
                    dataType:"json",
                    contentType: "application/json; charset=utf-8",
                    data:JSON.stringify({
                        "id":id
                    }),
                    success:function (result) {
                        if(result.code=="200") {
                            $alert("删除成功!");
                            $("#main_table").datagrid("reload");
                        }else{
                            $alert(result.message);
                        }
                    }
                })
            })
        });
        return clickEvent;
    },
    searchInit:function () {
        $("#a_search").on("click",function () {
            $("#main_table").datagrid('load', {
                systemId: $('#select_system').combobox('getValue'),
                enable: $('#select_enable').combobox('getValue')
            })
        });
        return clickEvent;
    },
    detailInit:function () {
        $("body").on("click","a[name=roleName]",function () {
            var id = $(this).parents("tr").children(":first").text();
            window.location.href="role-detail?id="+id;
        });
        return clickEvent;
    }
};
$(function(){
    pageInit();
    mainTableInit();
});
function pageInit(){
    $("#a_create").show();
    clickEvent.modifyInit().startInit().stopInit().deleteInit().searchInit().detailInit();
}
function mainTableInit(){
    $("#main_table").datagrid({
        url:'api/v1/p/mms/authRole/list',
        border : false,
        height:$(window).height(),
        pagination : true,
        idField : 'id',
        pageSize : 20,
        pageList : [ 20, 50,  100 ],
        checkOnSelect : true,//单击选中
        selectOnCheck : true,
        emptyMsg : "暂无数据",
        singleSelect : true,
        rownumbers:true,
        columns:[[
            {
                field : 'id',
                hidden:true
            },{
                field:'roleName',
                title:'角色名称',
                width:200,
                formatter:function (value,row,index) {
                    return "<a href='javascript:;' name='roleName'>"+value+"</a>"
                }
            },{
                field:'issys',
                title:'是否是系统级别',
                width:200,
                formatter:function (value,row,index) {
                    return issys[value];
                }
            },
            {
                field:'systemName',
                title:'所属系统',
                width:200
            },{
                field:'enable',
                title:'启用状态',
                width:200,
                formatter:function (value,row,index) {
                    return enable[value];
                }
            },{
                field:'opertation',
                title:'操作',
                hidden:isSale,
                width:300,
                formatter:function (value,row,index) {
                    if(row.enable=="0"){
                        return "<a href='javascript:;' name='start'>启用</a>|<a href='javascript:;' name='modify'>修改</a>|<a href='javascript:;' name='delete'>删除</a>";
                    }else{
                        return "<a href='javascript:;' name='stop'>停用</a>|<a href='javascript:;' name='modify'>修改</a>|<a href='javascript:;' name='delete'>删除</a>";
                    }
                }
            }
        ]],
        loadFilter:function(data){
            if(data.code!='200'){
                return data.data;
            }
            return data.data;
        },
        onLoadSuccess:function(data){

        },
        toolbar:"#tb"
    })
}